package team.se.tms.controller.role;

import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import team.se.tms.annotation.CheckFormat;
import team.se.tms.annotation.Regex;
import team.se.tms.controller.BaseController;
import team.se.tms.dto.Page;
import team.se.tms.dto.RoleDetail;
import team.se.tms.exception.DeleteException;
import team.se.tms.exception.InsertException;
import team.se.tms.exception.UpdateException;
import team.se.tms.po.AuthorityInfo;
import team.se.tms.service.AuthorityService;
import team.se.tms.service.RoleService;
import team.se.tms.service.UserInfoService;
import team.se.tms.util.ResultUtil;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 角色的控制层
 *
 * @author johnny
 * @date 2018/12/20
 */
@Controller
@RequestMapping("role")
public class RoleInfoController extends BaseController{
    @Autowired
    AuthorityService authorityService;
    @Autowired
    RoleService roleInfoService;
    @Autowired
    UserInfoService userInfoService;


    /**
     * 去添加角色界面
     *
     * @return ModelAndView 页面和返回值
     * @throws Exception 异常
     * @create on 2018/11/24 by johnny
     */
    @RequestMapping("toAdd")
    public ModelAndView toAddRole() throws Exception{
        ModelAndView mv = this.getModelAndView();
        List<AuthorityInfo> authorityInfoList =  roleInfoService.listAllAuthorityInfos();
        Gson gson =new Gson();
        String allAuthorityInfosJson = gson.toJson(authorityInfoList);
        mv.addObject("allAuthorityInfosJson",allAuthorityInfosJson);
        mv.addObject("dotype","add");
        mv.setViewName("role/modify_role");
        return mv;
    }
    /**
     * 添加角色
     *
     * @param roleDetail 角色信息
     * @return Object 添加结果
     * @throws Exception 异常
     * @create on 2018/12/20 by johnny
     */
    @RequestMapping("add")
    @ResponseBody
    @CheckFormat({"roleId","authorityName","authorityParentId","authorityBaseId"})
    public Object addRole(@RequestBody RoleDetail roleDetail) throws Exception{
        Boolean result = roleInfoService.saveRole(roleDetail);
        if(!result){
            throw new InsertException();
        }
        return ResultUtil.success();
    }
    /**
     * 去角色列表界面
     *
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2018/11/24 by johnny
     */
    @RequestMapping("list")
    public String toViewAllRoles() throws Exception{
        return "role/role_list";
    }
    /**
     * 分页查询角色列表
     *
     * @param page 分页
     * @return String 角色列表的json字符串
     * @throws Exception 异常
     * @create on 2018/12/25 by johnny
     */
    @RequestMapping("getList")
    @ResponseBody
    public String searchAllRoles(Page page, HttpSession session) throws Exception{
        List<RoleDetail> roleDetailList
                = roleInfoService.listAllRoleDetails(page);
        session.setAttribute("roleDetailList",roleDetailList);
        return page.getDataTabelJson(roleDetailList);
    }

    /**
     * 去修改角色界面
     *
     * @param roleId 角色id
     * @return ModelAndView 页面和返回值
     * @throws Exception 异常
     * @create on 2018/11/24 by johnny
     */
    @RequestMapping("toEdit")
    public ModelAndView toEditRole(String roleId,HttpSession session) throws Exception{
        List<RoleDetail> roleDetailList=(List<RoleDetail>)session.getAttribute("roleDetailList");
        RoleDetail roleDetail =new RoleDetail();
        //从sesion中找到id为rodleId的roleDetail
        for(RoleDetail item:roleDetailList){
            if(roleId.equals(item.getRoleId())){
                roleDetail = item;
                break;
            }
        }
        ModelAndView mv= this.getModelAndView();
        Gson gson =new Gson();
        //把权限转化为json字符串
        String authoritysJson =gson.toJson(roleDetail.getAuthorityInfos());

        List<AuthorityInfo> authorityInfoList =  roleInfoService.listAllAuthorityInfos();
        String allAuthorityInfosJson = gson.toJson(authorityInfoList);

        mv.addObject("allAuthorityInfosJson",allAuthorityInfosJson);
        mv.addObject("roleDetail",roleDetail);
        mv.addObject("authoritysJson",authoritysJson);
        mv.addObject("dotype","edit");
        mv.setViewName("role/modify_role");

        return mv;
    }
    /**
     * 修改角色
     *
     * @param roleDetail 待修改角色信息
     * @return Object 修改结果
     * @throws Exception 异常
     * @create on 2018/11/25 by johnny
     */
    @RequestMapping("edit")
    @ResponseBody
    @CheckFormat({"authorityName","authorityParentId","authorityBaseId"})
    public Object editRole(@RequestBody  RoleDetail roleDetail,HttpSession session) throws Exception{
        List<RoleDetail> roleDetailList=(List<RoleDetail>)session.getAttribute("roleDetailList");
        try {
            roleInfoService.modifyRole(roleDetail);
        }catch (Exception e){
            throw new UpdateException();
        }

        return ResultUtil.success();
    }

    /**
     * 删除角色
     *
     * @param roleId 角色id
     * @return Object 删除结果
     * @throws Exception 异常
     * @create on 2018/11/25 by johnny
     */
    @RequestMapping("drop")
    @ResponseBody
    @CheckFormat
    public Object dropRole(@Regex("^[A-Za-z0-9]{16}$") String roleId) throws Exception{
        boolean result=roleInfoService.removeRole(roleId);
        if(!result){
            throw new DeleteException();
        }
        return ResultUtil.success();
    }

}
